package com.itheima.health.controller;



import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/checkgroup")
public class CheckgroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    /**
     *  检查组添加
     * @param checkGroup
     * @param checkitemIds
     * @return
     */ // http://localhost:18080/api/checkgroup/add?checkitemIds=39,40,41,57,56
      @PostMapping("/add")
      public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
          log.info("[检查组添加]"+checkGroup+checkitemIds);
           // 插入数据组
          checkGroupService.add(checkGroup,checkitemIds);

            return  new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS );
      }


    /**
     *  检查组分页
     * @param queryPageBean
     * @return
     */
     @GetMapping("/findPage")
     public Result findPage(QueryPageBean queryPageBean){
         log.info("[检查项-分页查询]data:{}"+queryPageBean);

         // 调用分页的业务方法
         PageResult pageResult = checkGroupService.pageQuery(queryPageBean);

         return new Result(true,MessageConst.ACTION_SUCCESS,pageResult);
     }


    /**
     *  检查组基本信息回显
     * @param id
     * @return
     */
     //  http://localhost:18080/api/checkgroup/findById?id=5
    @GetMapping("/findById")
    public Result findById(Integer id){
     log.info("[查询的Id为:===>]"+id);
     CheckGroup CheckGroupId = checkGroupService.findById(id);
      return  new Result(true,MessageConst.ACTION_SUCCESS,CheckGroupId);
    }


    /**
     * 检查组检查项信息回显
     * @param id
     * @return
     */ // http://localhost:18080/api/checkgroup/findCheckItemIdsByCheckGroupId?id=5
    @GetMapping("/{id}")
   public  Result findCheckItemIdsByCheckGroupId(Integer  id){
               log.info("[检查组id]:====>"+id);
        List<Integer> checkItemIdsByCheckGroupList = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        return  new Result(true,MessageConst.ACTION_SUCCESS,checkItemIdsByCheckGroupList);
   }

    /**
     *   检查组修改
     * @param checkGroup
     * @param checkitemIds
     * @return
     */   //  http://localhost:18080/api/checkgroup/edit?checkitemIds=80,81,82,83,84,85,86,87,88,89,90,91,92
     @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
            log.info("[修改时需要清空的id]===>"+checkGroup+checkitemIds);

         checkGroupService.updateAll(checkGroup,checkitemIds);

        return  new Result(true,MessageConst.ACTION_SUCCESS);
    }


    /**
     * 根据id删除检查组
     * @param id
     * @return
     */ // http://localhost:18080/api/checkgroup/deleteCheckGroupitemById?id=33
        @PostMapping("/{id}")
        public Result deleteCheckGroupitemById(Integer id){
            log.info("[删除id]===>"+id);
            checkGroupService.delete(id);
            return  new Result(true,MessageConst.ACTION_SUCCESS);
        }


       // http://localhost:18080/api/checkgroup/findAll
      @GetMapping("/findAll")
       public Result findAll(){
       log.info("[检查组展示]");
          List<CheckGroup> checkGroups = checkGroupService.selectAll();
          return  new Result(true,MessageConst.ACTION_SUCCESS,checkGroups);
       }


}
